/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import model.dao.DisciplinaDao;
import model.dao.DisciplinaDaoImpl;
import model.pojo.Disciplina;

/**
 *
 * @author rodricxc
 */
public class CadastrarDisciplina extends javax.swing.JDialog {

    /**
     * Creates new form CadastrarDisciplina
     */
    public CadastrarDisciplina(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painelCadstro = new javax.swing.JPanel();
        labelDisciplina = new javax.swing.JLabel();
        labelCargaH = new javax.swing.JLabel();
        labelEmenta = new javax.swing.JLabel();
        botaoCadastrar = new javax.swing.JButton();
        campoTextoCargaH = new javax.swing.JFormattedTextField();
        campoTextoDisciplina = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        areaTextoEmenta = new javax.swing.JTextArea();
        painelMensagem = new javax.swing.JPanel();
        labelMensagem = new javax.swing.JLabel();
        botaoSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cdastro Disciplina");

        painelCadstro.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro"));

        labelDisciplina.setText("Disciplina:");

        labelCargaH.setText("Carga horária:");

        labelEmenta.setText("Ementa:");

        botaoCadastrar.setText("Cadastrar");
        botaoCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCadastrarActionPerformed(evt);
            }
        });

        campoTextoCargaH.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        areaTextoEmenta.setColumns(20);
        areaTextoEmenta.setRows(5);
        jScrollPane1.setViewportView(areaTextoEmenta);

        javax.swing.GroupLayout painelCadstroLayout = new javax.swing.GroupLayout(painelCadstro);
        painelCadstro.setLayout(painelCadstroLayout);
        painelCadstroLayout.setHorizontalGroup(
            painelCadstroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelCadstroLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelCadstroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(painelCadstroLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botaoCadastrar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, painelCadstroLayout.createSequentialGroup()
                        .addGroup(painelCadstroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelDisciplina)
                            .addComponent(labelEmenta))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(painelCadstroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
                            .addGroup(painelCadstroLayout.createSequentialGroup()
                                .addComponent(campoTextoDisciplina)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(labelCargaH)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoTextoCargaH, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        painelCadstroLayout.setVerticalGroup(
            painelCadstroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelCadstroLayout.createSequentialGroup()
                .addGroup(painelCadstroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDisciplina)
                    .addComponent(campoTextoDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoTextoCargaH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCargaH))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(painelCadstroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelCadstroLayout.createSequentialGroup()
                        .addComponent(labelEmenta)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(painelCadstroLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botaoCadastrar)
                        .addGap(0, 6, Short.MAX_VALUE))))
        );

        painelMensagem.setBorder(javax.swing.BorderFactory.createTitledBorder("Mensagem"));

        labelMensagem.setText(" ");

        javax.swing.GroupLayout painelMensagemLayout = new javax.swing.GroupLayout(painelMensagem);
        painelMensagem.setLayout(painelMensagemLayout);
        painelMensagemLayout.setHorizontalGroup(
            painelMensagemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelMensagemLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelMensagem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        painelMensagemLayout.setVerticalGroup(
            painelMensagemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelMensagemLayout.createSequentialGroup()
                .addComponent(labelMensagem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        botaoSair.setText("Voltar");
        botaoSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(painelMensagem, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(painelCadstro, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botaoSair, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelCadstro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(painelMensagem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(botaoSair)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSairActionPerformed
       this.dispose();
    }//GEN-LAST:event_botaoSairActionPerformed

    private void botaoCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCadastrarActionPerformed
       
        Integer a;
        try{
            a = Integer.parseInt(campoTextoCargaH.getText());
        }catch(Exception e){
            a = 0;
        }
        Disciplina d = new Disciplina(campoTextoDisciplina.getText(),
                areaTextoEmenta.getText(), a);
        DisciplinaDao dDao = DisciplinaDaoImpl.getInstance();
        //dDao.add(new Disciplina("GA", "sdkgasd", 72));
        //*
        if((d.getEmenta().equals(""))||(d.getCargaHoraria() == 0)||(d.getNome().equals(""))){
            labelMensagem.setText("Erro, Valores inválidos!");
        }else if( dDao.add(d) ){
            labelMensagem.setText("Disciplina adicionada com sucesso!");
            campoTextoCargaH.setText("");
            campoTextoDisciplina.setText("");
            areaTextoEmenta.setText("");
        }else{
            labelMensagem.setText("Erro, Disciplina nao adicionada!");
        }//*/
    }//GEN-LAST:event_botaoCadastrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastrarDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastrarDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastrarDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastrarDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CadastrarDisciplina dialog = new CadastrarDisciplina(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea areaTextoEmenta;
    private javax.swing.JButton botaoCadastrar;
    private javax.swing.JButton botaoSair;
    private javax.swing.JFormattedTextField campoTextoCargaH;
    private javax.swing.JTextField campoTextoDisciplina;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCargaH;
    private javax.swing.JLabel labelDisciplina;
    private javax.swing.JLabel labelEmenta;
    private javax.swing.JLabel labelMensagem;
    private javax.swing.JPanel painelCadstro;
    private javax.swing.JPanel painelMensagem;
    // End of variables declaration//GEN-END:variables
}
